package com.lh.api.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lh.api.dao.UserAddressMapper;
import com.lh.api.form.AddressForm;
import com.lh.api.form.DefaultAddressForm;
import com.lh.api.service.UserAddressService;
import com.lh.api.vo.UserAddrVo;
import com.lh.common.entity.admin.UserAddress;
import com.lh.common.enums.IsDel;
import com.lh.common.enums.TrueFalseEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserAddress addObj(AddressForm addressForm, Long userId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        userAddress.setRecipients(addressForm.getRecipients());
        userAddress.setPhone(addressForm.getPhone());
        userAddress.setRegion(addressForm.getRegion());
        userAddress.setDetailed(addressForm.getDetailed());
        userAddress.setUpdateTime(new Date());
        userAddress.setCreateTime(new Date());
        userAddress.setIsDel(IsDel.NO);
        userAddress.setIsDefault(TrueFalseEnum.FALSE);

        List<UserAddrVo> userAddrVos = selectByUserId(userId);
        if(CollectionUtils.isEmpty(userAddrVos)){
            userAddress.setIsDefault(TrueFalseEnum.TRUE);
        }

        baseMapper.insert(userAddress);
        return  userAddress;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateObj(AddressForm addressForm) {
        UserAddress userAddress = new UserAddress();
        userAddress.setRecipients(addressForm.getRecipients());
        userAddress.setPhone(addressForm.getPhone());
        userAddress.setRegion(addressForm.getRegion());
        userAddress.setDetailed(addressForm.getDetailed());
        userAddress.setUaid(addressForm.getUaid());
        userAddress.setUpdateTime(new Date());
        return baseMapper.updateByKey(userAddress);
    }

    @Override
    public List<UserAddrVo> selectByUserId(Long userId) {
        if (userId == null ){
            return new ArrayList<>();
        }
        return baseMapper.selectByUserId(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setDefault(DefaultAddressForm defaultAddressForm, Long userId) {
        if (userId == null ){
            return;
        }
        baseMapper.chanelDefault(userId);

        UserAddress userAddress = new UserAddress();
        userAddress.setUaid(defaultAddressForm.getUaid());
        userAddress.setIsDefault(TrueFalseEnum.TRUE);
        baseMapper.updateByKey(userAddress);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delAddress(Long userId, Long uaid) {
        if (userId == null || uaid == null){
            return;
        }
        UserAddress userAddress = baseMapper.selectById(uaid);
        if(userAddress == null || IsDel.YES.equals(userAddress.getIsDel())){
            return;
        }
        baseMapper.delAddress(userId, uaid);
        //如果删除的是默认地址，要将第一个设置为默认地址
        if(TrueFalseEnum.FALSE.equals(userAddress.getIsDefault())){
            return;
        }

        List<UserAddrVo> userAddrVos = selectByUserId(userId);
        if(CollectionUtils.isEmpty(userAddrVos)){
            return;
        }

        UserAddress temp = new UserAddress();
        temp.setUaid(userAddrVos.get(0).getUaid());
        temp.setIsDefault(TrueFalseEnum.TRUE);
        baseMapper.updateByKey(temp);
    }
}
